<?php
/**
 * Bayon helper
 * 
 * @author Prak Sophy, sophy.prak@gmail.com
 * 
 */
class BnoticeHelper extends Helper {
	
/**
 * Include helpers
 *
 * @var array
 */	
	public $helpers = array( 'Html','Bayon', 'Js' );
	
	public function displayNotice($notices, $profile = false){
		$notice_text = '';
		$myId = $this->Bayon->userId();
		$myRT = array();
		
		if( !empty($notices) ){
			
			foreach( $notices as $n){
				
				
				$user = $this->Html->tag('span', 
					$this->Html->link($n['User']['username'], 
					'/'.$n['User']['username']), array('class' => 'author-username')
				);

                                //thumb author
                                $thumbAuthor = $this->Html->tag('span', $this->Html->image($n['avatar'], array('url' => '/'.$n['User']['username'])), array( 'class' => 'author-thumb'));
				
				//favor 
				$favor = $this->Html->tag('li', $this->favor($n['Favor'], $n['Notice']['id']), array('id' => 'favor_'.$n['Notice']['id']));
				
				//Repeated
					if($n['Notice']['repeat_of'] != null && $n['Notice']['user_id'] == $myId){
						$myRT[$n['Notice']['repeat_of']] = 1;	
					}
					
					if( $n['Notice']['user_id'] !== $myId)
						$repeat = $this->Html->tag('li', $this->repeatLink($n['User']['username'], $n['Notice']['id'], $myRT), array('id' => 'repeat_'.$n['Notice']['id']));
					else 	
						$repeat = '';
				
				if( !$profile && $this->Bayon->isLogging()){
							
					//setup action
                                        $actions = $this->Html->tag('ul' ,$favor.' '. $repeat .' '.$this->noticeOption($n['User']['username'], $n['Notice']['id']), array('class' => 'actions actions-hidden clearfix', 'id' => 'ul_notice_'.$n['Notice']['id']));
                                        $actions = $this->Html->div('actions-wrap', $actions );
					$notice_text .= $this->Html->tag(
						'li',
						$thumbAuthor . sprintf( '<div class="message-notice">%s<span class="message">%s</span> %s</div>',$user ,$n['Notice']['render'], $actions),
						array('id' => 'notice_'.$n['Notice']['id'], 'class' => 'notice clearfix')
					);
				}elseif($this->Bayon->isLogging()){
					//setup action
                                        $actions = $this->Html->tag('ul' ,$favor.' '. $repeat .' '.$this->noticeOption($n['User']['username'], $n['Notice']['id']),  array('class' => 'actions actions-hidden clearfix', 'id' => 'ul_notice_'.$n['Notice']['id']));
                                        $actions = $this->Html->div('actions-wrap', $actions );
					$notice_text .= $this->Html->tag(
						'li',
						$thumbAuthor . sprintf( '<div class="message-notice">%s <span class="message">%s</span> %s</div>',$user ,$n['Notice']['render'], $actions),
						array('id' => 'notice_'.$n['Notice']['id'], 'class' => 'notice clearfix')
					);		
							
				}else{
					$notice_text .= $this->Html->tag(
						'li',
						sprintf('%s <div class="message-notice">%s<span class="message">%s</span></div>',$thumbAuthor ,$user,$n['Notice']['render']),
						array('id' => 'notice_'.$n['Notice']['id'], 'class' => 'notice clearfix')
					);		
				}
				
				
				
				
			}
			
		}

		return $this->Html->tag('ul', $notice_text, array( 'id' => 'section_notice') );
	}
		
/**
 * favor notice use to add or remove favor
 * 
 * @param array $favors
 * @return string of link add or remove Favor
 * 
 */
	public function favor($favors, $noticeId, $single = false){
		$myId = $this->Bayon->userId();
		$myFavor = false;
		if($favors != null && $single == false){
			foreach($favors as $favor){
				if($favor['user_id'] == $myId){
					$myFavor = true;
					break;
				}
			}
		}
		if($myFavor == true || $single == true){
			$favor = $this->Js->link(__('Disfavor', true),
				array(
						'controller' => 'favors',
						'action' => 'disfavor',
						
					),
					array(
						'method' => 'POST',
						'data' => 'data[Favor][notice_id]='.$noticeId,
						'error' => 'alert("error")',
						'type' => 'html',
						'success' => 'disfavor(data,'.$noticeId.')',
						'escape' => false,
                                                'title' => 'Disfavor this notice'
					)
			);
		}else{
			$favor = $this->Js->link(__('Favor', true),
				array(
						'controller' => 'favors',
						'action' => 'add',
						
					),
					array(
						'method' => 'POST',
						'data' => 'data[Favor][notice_id]='.$noticeId,
						'error' => 'alert("error")',
						'type' => 'html',
						'success' => 'favor(data, '.$noticeId.')',
						'escape' => false,
                                                'title' => 'Favor this notice'
					)
			);
		}
		return sprintf('<span class="ui-icon ui-icon-heart"></span> %s', $favor);
	}	
	public function noticeOption( $username, $noticeId, $home = true ){
		
		$reply ='';
		$remove ='';
		
			if($username === $this->Bayon->username() ){
				$remove = $this->removeNotice($noticeId);
			}else{
							
				$reply = $this->Js->link(__('Reply', true), 
				array(
					'controller' => 'notices',
					'action' => 'reply'
					
				),
				array(
					'method' => 'POST',
					'data' => 'data[Reply][user_notice]='.$username.'_'.$noticeId,
					'error' => 'alert("error")',
					'type' => 'json',
					'success' => 'reply(data)',
					'escape' => false
					)
			);
                        $reply = sprintf('<li><span class="ui-icon ui-icon-arrowreturnthick-1-w"></span>%s</li>', $reply);
		}
		if($home){
                    return $reply.$remove;
			
		}else{
                    return $remove; 
		}
		
	}

	
	public function removeNotice($noticeId){
		$remove = $this->Js->link(__('Remove', true),
				array(
						'controller' => 'notices',
						'action' => 'remove',
						
					),
					array(
						'method' => 'POST',
						'data' => 'data[Remove][notice_id]='.$noticeId,
						'error' => 'alert("error")',
						'type' => 'json',
						'success' => 'removeNotice(data)',
						'escape' => false
					)					
			);
		return sprintf('<li><span class="ui-icon ui-icon-close"></span>%s</li>', $remove);
	}
	
	
/**
 * return string link of repeate notice
 * 
 */
	public function repeatLink( $username, $noticeId, $myRT ){
		if(isset($myRT[$noticeId])){
				$repeat = __('Repeated', true);
		}else{
			
			$repeat = $this->Js->link(__('Repeat', true),
				array(
						'controller' => 'notices',
						'action' => 'repeated'
					),
					array(
					'method' => 'POST',
					'data' => 'data[Repeated][user_notice]='.$username.'_'.$noticeId,
					'error' => 'alert("error")',
					'type' => 'json',
					'success' => 'repeated(data, "'.$noticeId.'", "'.__('Repeated', true).'")',
					'escape' => false
					)
			);
		}
		return sprintf('<span class="ui-icon ui-icon-refresh"></span>%s', $repeat);
	}	
}
?>	